package com.practice.bootdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.practice.bootdemo.dto.ChannelUserDto;
import com.practice.bootdemo.dto.GoEasyResp;
import com.practice.bootdemo.dto.GoEasyUserChannelDto;
import io.goeasy.GoEasy;
import io.goeasy.publish.GoEasyError;
import io.goeasy.publish.PublishListener;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author :Administrator
 * @description: TODO edit class Description
 * @create date: 2021/10/29 18:25
 */
@RestController
@RequestMapping(value = "send")
@Slf4j
public class GoEasySendTestController {

    final String appkey = "BC-843388ae9cec402eb35090aae5383c7a";

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "sendmsg", method = RequestMethod.GET)
    @ApiOperation(value = "发送测试信息", httpMethod = "GET")
    public void sendmsg(String msg,String channel) {
        try {
            sendMsg(msg ,channel);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "getUsers", method = RequestMethod.GET)
    @ApiOperation(value = "getUsers", httpMethod = "GET")
    public List<ChannelUserDto> getUsers(String channel) {
        String url="http://rest-hangzhou.goeasy.io/v2/pubsub/herenow?".concat("appkey=").concat(appkey)
        .concat("&channel=".concat(channel)
        .concat("&includeUsers=true"));
//        paramMap.put("distinct","false");
        String text = restTemplate.getForEntity(url, String.class).getBody();
        GoEasyResp respDto = JSONObject.parseObject(text, GoEasyResp.class);
        if (respDto.getCode().equals("200")){
            Map content = respDto.getContent();
            Map channels = (Map) content.get("channels");
            String userInfo = JSONObject.toJSONString(channels.get(channel));
            GoEasyUserChannelDto userChannelDto = JSONObject.parseObject(userInfo, GoEasyUserChannelDto.class);
            List<ChannelUserDto> users = userChannelDto.getUsers();
            return users;
        }
        return null;
    }

    public void sendMsg(String msg,String channel) throws InterruptedException {
        GoEasy goEasy = new GoEasy("http://rest-hangzhou.goeasy.io", "BC-843388ae9cec402eb35090aae5383c7a");
            goEasy.publish(channel,msg , new PublishListener() {
                @Override
                public void onSuccess() {
                    log.info("---------|onSuccess | channel={} msg={}",channel,msg);
                }

                @Override
                public void onFailed(GoEasyError error) {
                    System.out.println("Failed to Publish message, error:" + error.getCode() + " , " + error.getContent());
                }
            });
    }
}
